<?php

class ucr {
    private static function _builderParams($data = array() , $key , $basesign) {
    	ksort($data);
    	$str = "";
    	foreach($data as $k=>$v){
    		$str = $str . $k . "=" . $v . "&";
    	}
    	
    	$str = $str . "key={$key}&basesign={$basesign}";

    	return $str;
    }
    
    private function callPayApi($method,$params,$requestType="POST"){
    	$app_id = C('PAY_APP_ID');
    	$app_key = C('PAY_APP_KEY');
    	 
    	$timestamp = time();
    	$noncestr = \tools::getSign($timestamp . "qiyunxin");
    	 
    	$basesign = md5($app_key . $noncestr . $timestamp);
    	$basesign = strtoupper($basesign);
    	 
    	$signStr = self::_builderParams($params , $app_key , $basesign);
    	$apisign = md5($signStr);
    	$apisign = strtoupper($apisign);
    	 
    	$sign = $basesign . "." . $apisign;
    	 
    	$header = [];
    	$header['app_id'] = $app_id;
    	$header['sign'] = $sign;
    	$header['timestamp'] = $timestamp;
    	$header['noncestr'] = $noncestr;
    	 
    	$url = C('PAY_API') . $method;
    	$params = json_encode($params);
    	$result = \tools::http_request($url, $params, $requestType , null , $header);
    	$return_data = json_decode($result, true);
    	return $return_data;
    }
    
    
 
    //绑定用户到UCR
    public static function bindUser($custid,$paypwd=null) {
    	if(empty(C('UCR_REG'))) return array();
    	 
    	$custid = (int)$custid;
    	$app_id = C('UCR_APP_ID');
    	$app_key = C('UCR_APP_KEY');
    
    	$timestamp = time();
    	$noncestr = \tools::getSign($timestamp . "qiyunxin");
    	 
    	$basesign = md5($app_key . $noncestr . $timestamp);
    	$basesign = strtoupper($basesign);
    	 
    	$params = [];
    	$params['r_id'] = (string)$custid;
    	$apisign = md5(self::_builderParams($params , $app_key , $basesign));
    	$apisign = strtoupper($apisign);
    	 
    	$sign = $basesign . "." . $apisign;
    	 
    	$header = [];
    	$header['app_id'] = $app_id;
    	$header['sign'] = $sign;
    	$header['timestamp'] = $timestamp;
    	$header['noncestr'] = $noncestr;
    	 
    	$url = C('UCR_API') . "/users/auth";
    	$params = json_encode($params);
    	$result = \tools::http_request($url, $params, 'POST' , null , $header);
    	$return_data = json_decode($result, true);
    	
    	//self::openPay($return_data['open_id'] , $paypwd);//自动开通
    	
    	return $return_data;
    }
    
    
//    //开通支付
//    public static function openPay($open_id , $paypwd=null) {
//    	$params = [];
//    	$params['open_id'] = $open_id;
//    	if(!empty($paypwd))
//    		$params['password'] = $paypwd;
//    	return self::callPayApi("/pay/bind",$params);
//    }
    
    
    
    
    //统一下单接口
    /* $out_trade_no 交易号
     * $amount 交易金额
    * $trade_type 交易类型(1.充值 2.支付)
    * $pay_type 支付方式(1.支付宝 2.微信 3.账户余额)
    * $opr_open_ids 指定预付款代号可以操作的人
    * $title 交易标题
    * $remark 交易备注
    * $notify_url 回调地址,调用成功返回（{"err_code":0,err_msg:"OK"}）
    */
    public static function makePrepay($open_id,$out_trade_no,$amount,$trade_type,$pay_type,$opr_open_ids='',$title,$remark='',$notify_url=null) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	$params['out_trade_no'] = $out_trade_no;
    	$params['amount'] = $amount;
    	$params['trade_type'] = $trade_type;
    	$params['pay_type'] = $pay_type;
    	if(!empty($opr_open_ids)) $params['opr_open_ids'] = $opr_open_ids;
    	if(!empty($remark)) $params['title'] = $title;
    	if(!empty($remark)) $params['remark'] = $remark;
    	$params['client_ip'] = \tools::getClientIp();
    	if(!empty($notify_url)) $params['notify_url'] = $notify_url;
    	
    	return self::callPayApi("/pay/makeprepay",$params);
    }

    //pay_type 支付类型 1.支付宝 2.微信 4.账户支付
    //tradetype 1.充值 2.消费
    public static function makePayV2($open_id,$custid,$tradetype,$out_trade_no,$imprest_code,$pay_type,$notify_url=null,$pay_token=null) {
        $params = [];
        $params['open_id'] = $open_id;
        $params['out_trade_no'] = $out_trade_no;
        $params['out_trade_type'] = $tradetype;
        //$params['amount'] = $amount;
        $params['memo'] = (string)$custid;
        $params['imprest_code'] = $imprest_code;
        //$params['pay_token'] = $pay_token;
        //$params['out_trade_type'] = $trade_type;
        $params['pay_type'] = $pay_type;
        $params['client_ip'] = \tools::getClientIp();
        if(!empty($notify_url)) $params['notify_url'] = $notify_url;

        return self::callPayApi("/imprest/users/".$open_id."/pay?version=1&pay_token=".$pay_token,$params);
    }
    
    
    //预付款生成 
    /* $type 预付款类型 1.付款 2.收款
     * $amount 预付款金额
    * $opr_open_ids 指定预付款代号可以操作的人
    * $remark 交易备注
    */
    public static function makeImprest($open_id,$opr_open_ids='',$type,$amount,$title,$remark=null) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	if(!empty($opr_open_ids)) $params['opr_open_ids'] = $opr_open_ids;
    	//$params['type'] = $type;
    	$params['amount'] = $amount;
    	if(!empty($remark)) $params['title'] = $title;
    	if(!empty($remark)) $params['remark'] = $remark;
    	 
    	return self::callPayApi("/imprest/users/".$open_id."/make",$params);
    }
    
    
    //获取支付token 
    public static function getPayToken($open_id,$password) {
    	$params = [];
    	$params['password'] = $password;
    	return self::callPayApi("/useraccounts/".$open_id."/paytoken",$params);
    }
    
    
    //支付预付款 
    public static function payImprest($pay_token,$open_id,$code,$title,$remark=null) {
    	$params = [];
    	$params['pay_token'] = $pay_token;
    	$params['open_id'] = $open_id;
    	$params['code'] = $code;
    	if(!empty($remark)) $params['title'] = $title;
    	if(!empty($remark)) $params['remark'] = $remark;

    	return self::callPayApi("/pay/payimprest",$params);
    }
    //支付预付款(余额)
    public static function payImprestV2($custid,$open_id,$code,$notify_url=null,$pay_token=null) {

        return self::makePayV2($open_id,$custid,"2",$code,$code,4,$notify_url,$pay_token);
    }
    
    
    //设置支付密码
    public static function setPayPass($open_id,$oldpwd,$newpwd) {
    	$params = [];
    	$params['password'] = $newpwd;
    	
    	return self::callPayApi("/useraccounts/".$open_id."/password",$params);
    }
    
    
    //领取预付款 
    public static function getPreAmount($open_id,$code,$amount,$title,$remark=null) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	$params['code'] = $code;
    	$params['amount'] = $amount;
    	if(!empty($remark)) $params['title'] = $title;
    	if(!empty($remark)) $params['remark'] = $remark;
    	 
    	return self::callPayApi("/imprests/fetch",$params,"POST");
    }
    //领取预付款
    public static function getPreAmountV2($open_id,$code,$amount,$title,$remark=null) {
        $params = [];
        $params['open_id'] = $open_id;
        $params['imprest_code'] = $code;
        $params['amount'] = $amount;
        if(!empty($remark)) $params['title'] = $title;
        if(!empty($remark)) $params['remark'] = $remark;

        return self::callPayApi("/imprest/users/".$open_id."/fetch",$params);
    }
    
    
    //账户余额支付 
    public static function makePay($open_id,$code,$amount,$title,$remark=null) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	$params['code'] = $code;
    	$params['amount'] = $amount;
    	if(!empty($title)) $params['title'] = $title;
    	if(!empty($remark)) $params['remark'] = $remark;
    
    	return self::callPayApi("/imprests/fetch",$params);
    }
    
    //资金流水列表
    public static function getAmountList($open_id,$page_index,$page_size,$title="",$remark=null) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	//$params['page_index'] = $page_index;
    	//$params['page_size'] = $page_size;
    	if(!empty($title)) $params['title'] = $title;
    	if(!empty($remark)) $params['remark'] = $remark;
    
    	return self::callPayApi("/useraccounts/".$open_id."/record?page_index={$page_index}&page_size={$page_size}&title=".$title,$params,"GET");
    }
    
    //查询账户信息 
    public static function getAccount($open_id) {
    	$params = [];

    	return self::callPayApi("/useraccounts/".$open_id,$params,"GET");
    }
    
    //查询账户使用记录 
    public static function getTurnover($open_id,$page_index,$page_size) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	$params['page_index'] = $page_index;
    	$params['page_size'] = $page_size;
    
    	return self::callPayApi("/accountsrecord/user",$params);
    }
    
    //预付款退款
    public static function refund($code) {
    	$params = [];
    	$params['code'] = $code;
    
    	return self::callPayApi("/imprest/refund",$params);
    }
    
    //预提款 
    public static function makeCash($open_id,$amount,$title,$remark=null) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	$params['amount'] = $amount;
    	if(!empty($title)) $params['title'] = $title;
    	if(!empty($remark)) $params['remark'] = $remark;
    
    	return self::callPayApi("/makecashout",$params);
    }  
    
    //提款
    public static function cash($open_id,$code) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	$params['code'] = $code;
    
    	return self::callPayApi("/cashout",$params);
    }
    
    //根据code查询账户记录
    public static function getAccountByCode($open_id,$code) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	$params['code'] = $code;
    
    	return self::callPayApi("/accountsrecord/code",$params);
    }
    
    
    //根据sub_trade_no查询账户记录
    public static function getAccountByTradeNo($open_id,$sub_trade_no) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	$params['sub_trade_no'] = $sub_trade_no;
    
    	return self::callPayApi("/accountsrecord/subtradeno",$params);
    }

    //根据sub_trade_no查询账户记录
    public static function getAccountByTradeNoV2($open_id,$sub_trade_no) {
        $params = [];

        return self::callPayApi("/useraccounts/".$open_id."/imprestrecord/".$sub_trade_no,$params,"GET");
    }
    
    
    //预提款 
    public static function makeCashout($open_id,$amount,$title,$remark=null) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	$params['amount'] = $amount;
    	if(!empty($title)) $params['title'] = $title;
    	if(!empty($remark)) $params['remark'] = $remark;
    
    	return self::callPayApi("/makecashout",$params);
    }
    
    
    //提款 
    public static function cashout($open_id,$code,$pay_token) {
    	$params = [];
    	$params['open_id'] = $open_id;
    	$params['code'] = $code;
    	$params['pay_token'] = $pay_token;
    
    	return self::callPayApi("/cashout",$params);
    }
    
}


